package org.ciallo.easytokencore.context.model;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.ciallo.easytokencore.util.EasyTokenUtils;
import org.ciallo.easytokencore.util.VerificationUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Easy-Token JWT令牌包装类
 *
 * <p>核心功能：</p>
 * <ul>
 *   <li>封装JWT令牌的生成、验证和刷新操作</li>
 *   <li>支持短期和长期令牌的生成</li>
 *   <li>提供令牌payload的便捷操作方法</li>
 *   <li>自动验证令牌有效性</li>
 * </ul>
 *
 * <p>主要属性：</p>
 * <ul>
 *   <li>payload: 令牌负载数据</li>
 *   <li>token: 原始令牌字符串</li>
 *   <li>isVerified: 是否已验证</li>
 *   <li>isLasting: 是否为长期令牌</li>
 * </ul>
 *
 * <p>使用示例：</p>
 * <pre>
 * {@code
 * // 生成新令牌
 * EasyToken token = EasyContextHolder.getToken();
 * token.addPayload("userId", 123);
 * token.addPayload("username", "test");
 * String jwt = token.getNewToken();
 *
 * // 验证令牌
 * EasyToken verifiedToken = new EasyToken(jwt);
 * if(verifiedToken.isVerified()) {
 *     // 令牌有效
 * }
 * }
 * </pre>
 *
 * @Description: Easy-Token JWT令牌包装类
 * @Author: Jukomu
 * @Package: org.ciallo.easytokencore.context.model
 * @Project: easy-token-demo
 * @Version: 1.0.0
 * @name: EasyToken
 * @Date: 2025-05-16
 * @Filename: EasyToken
 * @see <a href="https://jwt.io/introduction/">JWT规范</a>
 */
public class EasyToken {

    private Map<String, Object> payload;
    private String token;
    private boolean isVerified;
    private boolean isLasting;

    public EasyToken() {
        init();
    }

    public EasyToken(String token) {
        init();
        this.token = token;
    }

    /**
     * 默认初始化
     */
    private void init() {
        this.payload = new HashMap<>();
        this.isVerified = false;
        this.isLasting = false;
    }

    /**
     * 验证token
     */
    private void verified() {
        DecodedJWT decodedJWT = EasyTokenUtils.getDecodedJWT(this.token);
        Map<String, Claim> claims = decodedJWT.getClaims();
        for (Map.Entry<String, Claim> claimEntry : claims.entrySet()) {
            if (claimEntry.getKey().equals("exp")) {
                continue;
            }
            payload.put(claimEntry.getKey(), claimEntry.getValue().as(Object.class));
        }
        this.isVerified = true;
    }

    /**
     * 判断token有效性
     */
    public boolean isVerified() {
        verified();
        return this.isVerified;
    }

    /**
     * 添加payload
     */
    public void addPayload(String key, Object value) {
        this.payload.put(key, value);
    }

    /**
     * 设置有效期长短
     * true-->有效期 1296000 秒, 15 天
     * false-->有效期 3600 秒
     */
    public void setLasting(boolean isLasting) {
        this.isLasting = isLasting;
    }

    /**
     * 生成新的token
     */
    public String getNewToken() {
        if (this.isLasting) {
            return EasyTokenUtils.getEasyLastingToken(this.payload);
        }
        return EasyTokenUtils.getEasyToken(this.payload);
    }


    /**
     * 刷新token
     */
    public String refreshToken() {
        // 获取有效期时间
        Object expireTime = this.payload.get("expireTime");
        if (VerificationUtils.isNull(expireTime)) {
            // 没有有效期时间,则默认短有效期token
            return EasyTokenUtils.getEasyToken(this.payload);
        }
        return EasyTokenUtils.getEasyToken(this.payload, (int) expireTime);
    }
}
